/**
 * 
 */
package com.css.crm.utility;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import com.css.crm.cache.CrmCache;
import com.css.crm.vo.CountryVo;
import com.css.crm.vo.PlaceVo;
import com.css.crm.vo.ProvinceVo;

/**
 * @author AIS
 * 
 */
public class PlaceUtil {

	public final static String PLACE_TYPE_CRM_PRVNC = "CRM_PRVNC";
	public final static String PLACE_TYPE_CRM_CITY = "CRM_CITY";

	public static void cacheAllPlace(List<PlaceVo> placeList) {
		List<CountryVo> countryList = new ArrayList<CountryVo>();

		if (placeList == null || placeList.size() < 1) {
			return;
		}

		List<ProvinceVo> provinceList = new ArrayList<>();
		List<PlaceVo> cityList = new ArrayList<>();

		Map<Long, CountryVo> countryMap = new HashMap<Long, CountryVo>();
		Map<Long, ProvinceVo> provinceMap = new HashMap<Long, ProvinceVo>();
		Map<Long, PlaceVo> cityMap = new HashMap<Long, PlaceVo>();

		CountryVo countryVo = null;
		ProvinceVo provinceVo = null;
		for (PlaceVo place : placeList) {
			cityMap.put(place.getPlaceId(), place);
			if (place.getParntPlaceId() == null) {
				// country place have no parent id
				countryVo = new CountryVo(place);
				countryList.add(countryVo);
				countryMap.put(place.getPlaceId(), countryVo);
			} else if (PLACE_TYPE_CRM_PRVNC.equals(place.getPlaceType())) {
				// province
				provinceVo = new ProvinceVo(place);
				provinceList.add(provinceVo);
				provinceMap.put(place.getPlaceId(), provinceVo);
			} else if (PLACE_TYPE_CRM_CITY.equals(place.getPlaceType())) {
				cityList.add(place);
			}
		}

		// take all city to province belong to
		for (PlaceVo city : cityList) {
			if (provinceMap.get(city.getParntPlaceId()) != null) {
				provinceVo = provinceMap.get(city.getParntPlaceId());
				provinceVo.getCityList().add(city);
			}
		}

		// take all province to country belong to
		for (ProvinceVo province : provinceList) {
			if (countryMap.get(province.getPlace().getParntPlaceId()) != null) {
				countryVo = countryMap.get(province.getPlace().getParntPlaceId());
				countryVo.getProvinceList().add(province);
			}
		}
		
		//set to cache
		CrmCache.countryMap = countryMap;
		CrmCache.provinceMap = provinceMap;
		CrmCache.countryList = countryList;
		CrmCache.cityMap = cityMap;
	}

	public static SelectItem[] convertCtryItem(List<CountryVo> countryList) {
		if (countryList == null || countryList.size() < 1) {
			return null;
		}
		SelectItem[] ctryItem = new SelectItem[countryList.size()];
		int i = 0;
		for (CountryVo country : countryList) {
			ctryItem[i++] = new SelectItem(country.getPlace().getPlaceId(), country.getPlace().getNamZht());
		}

		return ctryItem;
	}

	public static SelectItem[] convertProvinceItem(List<ProvinceVo> provinceList) {
		if (provinceList == null || provinceList.size() < 1) {
			return null;
		}
		SelectItem[] ctryItem = new SelectItem[provinceList.size()];
		int i = 0;
		for (ProvinceVo province : provinceList) {
			ctryItem[i++] = new SelectItem(province.getPlace().getPlaceId(), province.getPlace().getNamZht());
		}

		return ctryItem;
	}
	
	public static SelectItem[] convertCityItem(List<PlaceVo> cityList) {
		if (cityList == null || cityList.size() < 1) {
			return null;
		}
		SelectItem[] ctryItem = new SelectItem[cityList.size()];
		int i = 0;
		for (PlaceVo place : cityList) {
			ctryItem[i++] = new SelectItem(place.getPlaceId(), place.getNamZht());
		}

		return ctryItem;
	}

}
